/**
 * 今日已结页面的逻辑处理
 */
var todayUI = function() {
    var self = this;

    /**
     * 游戏选择页面
     */
    self.gameSelect = null;

    /**
     * 存放结果的table页
     */
    self.table = null;

    /**
     * 是手机版本还是PC版本
     */
    self.phone = false;

    self.btn = null;
};
todayUI.prototype = Object.create(PageList.prototype);

todayUI.prototype.init = function() {
    var self = this;
    self.fillGames();
    self.query();

    self.jumpPage = function(page) {
        self.query(page);
    };
    if (self.btn) {
        self.btn.click(function() {
            self.query();
        });
    }
}

todayUI.prototype.fillGames = function() {
    var self = this;
    if (!self.gameSelect) return;

    self.gameSelect.append('<option value="-1" selected>所有彩种</option>')
    var arr = get_game_list();
    for (var i in arr) {
        var line = '<option value="__v__">__name__</option>';
        line = line.replace(/__v__/, arr[i]);
        line = line.replace(/__name__/, get_game_display_name(arr[i]));
        self.gameSelect.append(line);
    }

    var currGame = urlInfo.params.game;
    if (currGame) {
        self.gameSelect.val(currGame);
        self.gameSelect.trigger('changed.selected.amui');
    }

    self.gameSelect.change(function() {
        self.query();
    });
};

todayUI.prototype.query = function(page) {
    var self = this;
    var game = self.gameSelect ? self.gameSelect.val() : (urlInfo.params.game || "bjpk10");
    if (game == '-1') game = undefined;
    page = page || 1;

    startLoading();
    CMD.bet_today_complete(game, page, self.getPageSize(), function(r) {
        stopLoading();
        self.fillPages(r.count, r.number, r.page, r.size);
        self.fillResults(r.list);
    });
}

todayUI.prototype.debugCheckSame = function() {
    var list = this.list;
    for (var i = 0; i < list.length - 2; i++) {
        var o = list[i];
        for (var j = i + 1; j < list.length; j++) {
            var o2 = list[j];
            if (o.issue == o2.issue && o.type == o2.type && o.target == o2.target) {
                console.error("重复单据", o, o2);
            }
        }
    }
}

todayUI.prototype.fillResults = function(list) {
    var self = this;
    self.list = list;
    if (!self.table) return;
    self.table.empty();

    if (Util.isEmpty(list)) {
        var col = self.phone ? 4 : 6;
        self.table.html("<tr><td colspan='" + col + "'>暂无数据！</td></tr>")
        $(".sum").html('下注总金额：<span class="red">0.00</span><br/>输赢总金额：<span class="red">0.00</span>');
        return;
    }

    var total = 0, profit = 0;
    for (var i in list) {
        var o = list[i];
        var tr = $("<tr></tr>");
        var target = Util.convertTarget(o.type, o.target);

        if (!self.phone) {
            tr.append("<td>" + new Date(o.time).Format("MM-dd HH:mm") + "</td>");
            tr.append("<td>" + get_game_display_name(o.type) + " 第<span class='blue'>" + o.issue + "</span>期</td>");
            tr.append("<td><span class='blue'>" + target + "</span>@<span class='gray'>" + o.rate + "</span></td>");
            tr.append("<td class='red'>" + (o.amount / 100) + "</td>");
            tr.append("<td>" + (o.back ? o.back*100 : 0) + "%</td>");
            if (o.profit >= 0)
                tr.append("<td class='red'>" + (o.profit/100).toFixed(3) + "</td>");
            else
                tr.append("<td class='green'>" + (o.profit/100).toFixed(3) + "</td>");
        }
        else {
            tr.append("<td>" + o.issue + "</td>");
            tr.append("<td>" + target + "</td>");
            tr.append("<td class='red'>" + (o.amount / 100) + "</td>");
            if (o.profit >= 0)
                tr.append("<td class='red'>" + (o.profit/100).toFixed(3) + "</td>");
            else
                tr.append("<td class='green'>" + (o.profit/100).toFixed(3) + "</td>");
        }
        
        total += o.amount;
        profit += o.profit;
        self.table.append(tr);
    }
    var t = (profit/100).toFixed(3);
    if (profit >= 0)
        $(".sum").html('本页下注总金额：<span class="red">' + (total/100).toFixed(2) +
            '</span><br/>本页输赢总金额：<span class="red">' + t +
            '</span>');
    else
        $(".sum").html('本页下注总金额：<span class="red">' + (total/100).toFixed(2) +
            '</span><br/>本页输赢总金额：<span class="green">' + t +
            '</span>');
}

window.TodayUI = new todayUI();
